<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>正向推理</title>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            color: #343a40;
            font-family: 'Arial', sans-serif;
        }
        .container {
            margin-top: 50px;
        }
        .card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        h1, h3 {
            color: #343a40;
        }
        .table-hover tbody tr:hover {
            background-color: #e9ecef;
            cursor: pointer;
        }
        .table .selected {
            background-color: #c3e6cb;
        }
        .scrollable-table {
            height: 400px;
            overflow-y: auto;
            border-radius: 8px;
        }
        .right-panel {
            display: flex;
            flex-direction: column;
            height: 100%;
        }
        .conclusion-container {
            flex-grow: 1;
            margin-bottom: 10px;
        }
        .button-container {
            text-align: center;
            margin-top: 10px;
        }
        #inferButton {
            background-color: #007bff;
            border-color: #007bff;
            border-radius: 5px;
            transition: background-color 0.3s, border-color 0.3s;
        }
        #inferButton:hover {
            background-color: #0056b3;
            border-color: #004085;
        }
        #conclusionText {
            border-radius: 8px;
            box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center mb-4">正向推理</h1>
        <div class="row">
            <!-- 左侧规则表格 -->
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h3>选择推理事实</h3>
                    </div>
                    <div class="card-body scrollable-table">
                        <table class="table table-hover">

                            <tbody id="rulesTable">
                                <!-- 动态填充规则 -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <!-- 右侧推理结论 -->
            <div class="col-md-8">
                <div class="card right-panel">
                    <div class="card-header">
                        <h3>推理结论</h3>
                    </div>
                    <div class="card-body conclusion-container">
                        <textarea id="conclusionText" class="form-control" rows="10" readonly></textarea>
                    </div>
                    <div class="button-container">
                        <button id="inferButton" class="btn btn-primary">开始推理</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(document).ready(function() {
            let selectedRules = new Set();

            // 获取规则列表
            function loadRules() {
                $.ajax({
                    url: '/infer_sys/operations/get_rules',
                    method: 'GET',
                    success: function(data) {
                        const rulesTable = $('#rulesTable');
                        rulesTable.empty();
                        data.rules.forEach(function(rule) {
                            rulesTable.append(
                                `<tr data-rule-name="${rule.name}">
                                    <td>${rule.name}</td>
                                </tr>`
                            );
                        });

                        // 绑定点击事件
                        $('tbody tr').click(function() {
                            const ruleName = $(this).data('rule-name');

                            if (selectedRules.has(ruleName)) {
                                selectedRules.delete(ruleName);
                                $(this).removeClass('selected');
                            } else {
                                selectedRules.add(ruleName);
                                $(this).addClass('selected');
                            }
                        });
                    }
                });
            }

            // 加载规则列表
            loadRules();

            // 推理按钮点击事件
            $('#inferButton').click(function() {
                if (selectedRules.size === 0) {
                    alert('请选择至少一个规则进行推理。');
                    return;
                }

                // 提交选中的规则
                $.ajax({
                    url: '/infer_sys/operations/forward_rules',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ rules: Array.from(selectedRules) }),
                    success: function(response) {
                        $('#conclusionText').val(response.msg);
                    }
                });
            });
        });
    </script>
</body>
</html>
